﻿using System;
using System.Collections.Generic;
using System.Text;
using System.IO;

namespace testProtobuf.Network
{
    class NetworkStream
    {
        byte[] _bytes = null;
        public byte[] getByteData()
        {
            return _bytes;
        }

        int _length = 0;

        public int getValidBufferLength()
        {
            return _length;
        }

        int _offset;
        public int getOffset()
        {
            return _offset;
        }

        public void addOffset(int value)
        {
            _offset += value;
        }

        int _bufferLength = 0;
        public int getTotalBufferLength()
        {
            return _bufferLength;
        }
        

        
        public bool Create(int bufferLength)
        {
            _offset = 0;
            _bufferLength = bufferLength;
            _bytes = new byte[bufferLength];
            return true;
        }

        public void Destroy()
        {
            _bytes = null;
        }

        public int GetLeftBufferLength()
        {
            return _bufferLength - _length;
        }

        public bool Append(byte[] tail, int l)
        {
            if (l + _length >= _bufferLength)
                return false;
            for (int i = 0; i < l; i++)
            {
                _bytes[i + _length] = tail[i];
            }

            _length += l;
            return true;
        }

        public int SeekTo(int offset)
        {
            if (offset < _length)
            {
                _offset = offset;
            }
            else
            {
                _offset = _length - 1;
            }



            return _offset;
        }

        public bool EraseData(int from, int size)
        {
            if (from < 0 || size <= 0 ||from + size >= _length)
                return false;

            int to = from + size;
            _length = _length - to;
            for (int i = 0; i < _length; i++)
            {
                _bytes[i] = _bytes[i + to];
            }

            _offset = 0;
            return true;
        }

       



    }
}
